package drafts

import (
	"fmt"
	draftsLogDto "news_for_deepseek_interface/dto/admin/drafts_log"
	"news_for_deepseek_interface/model"
	"news_for_deepseek_interface/model/drafts"
)

func List(params draftsLogDto.ListRequest) (response draftsLogDto.ListResponse, err error) {

	where := "1=1"
	db := model.GetInstance().GetDB()

	// 获取总数
	var total int64
	if err = db.Model(&drafts.DraftsLogsModel{}).Where(where).Count(&total).Error; err != nil {
		err = fmt.Errorf("数量查询失败%s", err.Error())
		return
	}

	// 分页查询
	offset := (params.Page - 1) * params.Limit
	var data []drafts.DraftsLogsModel
	if err = db.Model(&drafts.DraftsLogsModel{}).Where(where).
		Offset(offset).
		Limit(params.Limit).
		Order("created_at DESC").
		Find(&data).Error; err != nil {
		err = fmt.Errorf("数据查询失败%s", err.Error())
		return
	}

	// 构建响应
	response = draftsLogDto.ListResponse{
		List:  make([]draftsLogDto.DraftsLog, 0),
		Total: total,
		Page:  params.Page,
		Limit: params.Limit,
	}

	for _, v := range data {
		response.List = append(response.List, draftsLogDto.DraftsLog{
			Id:         v.Id,
			Date:       v.Date.Format("2006-01-02"),
			TotalNum:   v.TotalNum,
			SuccessNum: v.SuccessNum,
			ErrorNum:   v.ErrorNum,
			Status:     v.Status,
			Type:       v.Type,
		})
	}

	return
}
